package com.xw.audioplayer;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.xw.floatlib.FloatActionController;
import com.xw.floatlib.FloatMonkService;
import com.xw.floatlib.callback.MusicCallBack;
import com.xw.floatlib.MusicData;
import com.xw.floatlib.view.PlayerStateData;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity {

    private RecyclerView listView;
    private List<MusicData> musicData;

    private String[] musicItem = {"sdcard/Music/music4.mp3","sdcard/Music/music3.mp3","sdcard/Music/music2.mp3",
    "http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8",
    "http://pili-live-hls.hsyapp.sobeylingyun.com/xjhsyapp2019/sxakzhpd.m3u8",
    "http://lhttp.qingting.fm/live/5021861/64k.mp3",
    };

    private String[] imageItem = {"http://39.105.117.74:8085/uploads/tvradio/radio/timg.jpg",
            "http://39.105.117.74:8085/uploads/tvradio/radio/cctv1_20160328.png",
            "http://39.105.117.74:8085/uploads/tvradio/radio/c.png",
            "http://39.105.117.74:8085/uploads/tvradio/radio/timg.jpg",
            "http://39.105.117.74:8085/uploads/tvradio/radio/c.png",
            "http://39.105.117.74:8085/uploads/tvradio/radio/cctv1_20160328.png"
    };

    private Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = findViewById(R.id.listView);
        btn = findViewById(R.id.btn1);
        listView.setLayoutManager(new LinearLayoutManager(this));

        musicData = new ArrayList<>();
        for (int i = 0; i < musicItem.length; i++) {
            MusicData dataBean = new MusicData();
            dataBean.setTitle("标题" + i);
            dataBean.setMusicId(i+"");
            dataBean.setPathUrl(musicItem[i]);
            dataBean.setIamgePath(imageItem[i]);
            musicData.add(dataBean);
        }


        final MyAdapter myAdapter = new MyAdapter(musicData);
        listView.setAdapter(myAdapter);


        FloatMonkService.registerCallback("0",new MusicCallBack() {
            @Override
            public void updateProgress(PlayerStateData stateData) {
                int position = 0;
                for (int i = 0; i < musicData.size(); i++) {

                    if (musicData.get(i).getMusicId().equals(stateData.getMusicId())){
                        position  = i;
                        musicData.get(i).setPlaying(true);
                        musicData.get(i).setMaxLength(stateData.getMaxLength());
                        musicData.get(i).setProgress(stateData.getProgress());
                        break;
                    }
                }

                myAdapter.notifyItemChanged(position);
            }

            @Override
            public void MusicPlatState(boolean isPlaying) {
                if (isPlaying)
                    Toast.makeText(MainActivity.this, "开始播放", Toast.LENGTH_SHORT).show();
                else
                    Toast.makeText(MainActivity.this, "停止播放", Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void click(View view) {

        switch (view.getId()){

            case R.id.btn1:

                break;

            case R.id.btn2:
//                startActivity(new Intent(MainActivity.this,Main2Activity.class));
                break;
        }
    }


    @Override
    protected void onPause() {
        super.onPause();
        FloatActionController.getInstance().show();
    }

    @Override
    protected void onResume() {
        super.onResume();
        FloatActionController.getInstance().hide();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        FloatMonkService.unRegister("0");
    }
}
